<template>
  <div class="app-container">
    <div class="top">
      <el-input
        v-model="kwInput"
        placeholder="输入 关键词 进行筛选"
        class="input-with-select"
        clearable
        @keyup.native.enter="handleSearch"
      >
        <el-button slot="append" icon="el-icon-search" @click="handleSearch">搜索</el-button>
      </el-input>
      <div class="advanced-filter" @click="handleToggleAdvancedFilter">
        <span class="mt-no-select">高级筛选</span>
        <i class="el-icon-caret-bottom" style="color:#409EFF;" />
      </div>
      <div v-if="bool_advanced" class="mt-advanced">
        <el-row :gutter="5">
          <el-col :xl="5" :md="12" :sm="12" :xs="12">
            图片数量:
            <el-input v-model.number="tempAdvanced.images_min" placeholder="min" clearable type="number" />
            <el-input v-model.number="tempAdvanced.images_max" placeholder="max" clearable type="number" />
          </el-col>
          <el-col :xl="5" :lg="12" :sm="12" :xs="12">
            谷歌排名:
            <el-input v-model.number="tempAdvanced.rank_min" placeholder="min" clearable type="number" />
            <el-input v-model.number="tempAdvanced.rank_max" placeholder="max" clearable type="number" />
          </el-col>
          <el-col :xl="5" :lg="12" :sm="12" :xs="12">
            单词数量:
            <el-input v-model.number="tempAdvanced.words_min" placeholder="min" clearable type="number" />
            <el-input v-model.number="tempAdvanced.words_max" placeholder="max" clearable type="number" />
          </el-col>
          <el-col :xl="5" :lg="12" :sm="12" :xs="12">
            视频数量:
            <el-input v-model.number="tempAdvanced.videos_min" placeholder="min" clearable type="number" />
            <el-input v-model.number="tempAdvanced.videos_max" placeholder="max" clearable type="number" />
          </el-col>
          <el-col :xl="1" :lg="22" :sm="22" :xs="22">
            <el-button type="primary" size="mini" class="mt-filter-btn" @click="handleSearch">过滤</el-button>
          </el-col>
        </el-row>
      </div>
    </div>

    <div v-if="data.length" v-loading="listLoading" class="mt-content">
      <el-card v-for="item in data" :key="item.id" class="mt-card">
        <div class="mt-content-left">
          <el-row :gutter="5">
            <el-col :span="24" :title="'谷歌排名: ' + item.rank" class="mt-col">
              <svg-icon icon-class="google" />
              <span>{{ item.rank }}</span>
            </el-col>
            <el-col :span="24" :title="'词数: ' + item.words" class="mt-col">
              <svg-icon icon-class="top_word" />
              <span>{{ item.words }}</span>
            </el-col>
            <el-col :span="24" :title="'图片数: ' + item.images" class="mt-col">
              <svg-icon icon-class="top_image" />
              <span>{{ item.images }}</span>
            </el-col>
            <el-col :span="24" :title="'视频数: ' + item.videos" class="mt-col">
              <svg-icon icon-class="top_video" />
              <span>{{ item.videos }}</span>
            </el-col>
            <el-col :span="24" :title="'来源关键词: ' + item.kw" class="mt-col">
              <svg-icon icon-class="top_search" />
              <span>{{ item.kw }}</span>
            </el-col>
          </el-row>
        </div>
        <div class="mt-content-right">
          <h2 class="mt-h2" @click="handleShowContent(item)" v-html="item.title" />
          <span class="mt-description" v-html="item.description" />
          <div class="mt-margin"><a :href="item.url" target="_blank">{{ item.url }}</a></div>
          <div style="margin-right: 5px;float:right">
            <i class="el-icon-date">{{ item.update_at.split('T')[0] }}</i>
            <i class="el-icon-time">{{ item.update_at.split('T')[1].split('.')[0] }}</i>
          </div>
        </div>
      </el-card>

      <el-pagination
        v-if="data.length"
        background
        class="mt-pagination"
        :current-page="page"
        :page-sizes="[10,20,50,100]"
        :page-size="pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handlePageChange"
      />
    </div>
    <div v-else style="text-align: center;padding-bottom: 20px">无数据</div>
  </div>
</template>

<script>
import { dt, ShowResult } from '@/api/spider/top_content/result'

export default {
  data() {
    return {
      data: [],
      listLoading: true,
      projects: [],
      selected_project: null,
      rules: [],
      kwInput: '',
      order_by: '',
      order_desc: '',
      // pagination
      pagesize: 10,
      page: 1,
      total: 0,

      action: null,
      dialogVisible: false,
      title: '',
      temp: {
        input: '',
        project: null,
        rules: []
      },
      width: '50%',

      tempAdvanced: {
        images_min: null,
        images_max: null,
        rank_min: null,
        rank_max: null,
        words_min: null,
        words_max: null,
        videos_min: null,
        videos_max: null
      },
      bool_advanced: false
    }
  },
  computed: {
    seed() {
      return this.$route.params.seed_id || null
    }
  },
  created() {
    this.getList()
  },
  methods: {
    resetTemp() {
      this.tempAdvanced = {
        images_min: null,
        images_max: null,
        rank_min: null,
        rank_max: null,
        words_min: null,
        words_max: null,
        videos_min: null,
        videos_max: null
      }
    },
    getList() {
      this.listLoading = true
      dt(JSON.stringify(
        Object.assign({}, this.tempAdvanced, {
          seed: this.seed,
          kw: this.kwInput,
          pagesize: this.pagesize,
          page: this.page
        })))
        .then(response => {
          this.scrollTop()
          this.data = response.data.data
          this.listLoading = false
          this.total = response.data.total
        }).catch(err => {
          console.log(err)
          this.listLoading = false
        })
    },
    notify(message, duration) {
      this.$notify({
        title: '成功',
        message: message,
        type: 'success',
        duration: duration || 2000
      })
    },
    // pageSize 改变后触发 table 更新
    handleSizeChange(val) {
      this.pagesize = val
      this.getList()
    },
    // page 改变后触发 table 更新
    handlePageChange(val) {
      this.page = val
      this.getList()
    },
    // 点击搜索
    handleSearch() {
      this.page = 1
      this.getList()
    },
    filterMethod(query, item) {
      return item.rule.indexOf(query) > -1
    },
    handleToggleAdvancedFilter() {
      if (this.bool_advanced) {
        this.resetTemp()
      }
      this.bool_advanced = !this.bool_advanced
    },
    handleShowContent(item) {
      this.listLoading = true
      ShowResult(item.id)
        .then(response => {
          this.listLoading = false
          this.$alert(response.data.content, item.title, {
            customClass: 'mt-message',
            dangerouslyUseHTMLString: true
          })
        })
        .catch(e => {
          this.listLoading = false
        })
    },
    scrollTop() {
      window.scrollTo(0, 0)
    }
  }
}
</script>

<style scoped>
  .el-tag + .el-tag {
    margin-left: 10px;
  }

  .top >>> .el-select .el-input {
    width: 350px;
  }

  .top >>> .input-with-select .el-input-group__prepend {
    background-color: #fff;
  }

  .top {
    padding-left: 20px;
    margin-bottom: 30px;
    margin-top: 28px;
  }

  .input-with-select {
    width: calc(100% - 120px);
  }

  .mt-pagination {
    float: right;
    margin-top: 15px;
    margin-bottom: 20px;
  }

  .app-container >>> .el-dialog {
    margin-top: 48px !important;
    margin-bottom: 68px;
  }

  .mt-detail {
    text-decoration: underline;
    margin-right: 8px;
  }

  .mt-detail:hover, .svg-icon:hover {
    cursor: pointer;
  }

  .mt-dialog >>> .el-transfer-panel {
    width: calc(50% - 60px);
  }

  .mt-form >>> .el-select {
    width: 100%;
  }

  .mt-form >>> .el-input__inner {
    width: 100%;
  }

  .mt-dialog >>> .el-dialog {
    margin: 88px auto !important;
  }

  .advanced-filter {
    display: inline-block;
    font-size: 14px;
    margin-left: 20px
  }

  .mt-no-select {
    -moz-user-select: none; /*火狐*/
    -webkit-user-select: none; /*webkit浏览器*/
    -ms-user-select: none; /*IE10*/
    -khtml-user-select: none; /*早期浏览器*/
    user-select: none;
    color: #409EFF;
  }

  .mt-advanced {
    margin: 20px auto;
    margin-top: 5px;
    padding: 10px;
  }

  .mt-advanced >>> .el-input{
    width: 36%;
  }

  .mt-filter-btn {
    line-height: 18px;
    margin-left: 20px;
    float: right;
  }

  .mt-content {
    padding: 10px;
    margin: 20px;
  }
  .mt-content-left {
    float: left;
    width: 200px;
    display: inline-block;
    vertical-align: middle;
  }
  .mt-content-right {
    width: calc(100% - 210px);
    display: inline-block;
    padding: 4px;
  }

  .mt-card >>> .el-card__body {
    padding: 5px;
  }

  .mt-content >>> em {
    color: red;
  }

  .mt-content >>> a {
    color: #006621;
  }

  .mt-margin {
    margin-top: 10px;
    margin-bottom: 10px;
  }

  .mt-h2 {
    cursor: pointer;
    font-size: 18px;
    color: #1a0dab;
    margin-top: 10px;
    margin-bottom: 10px;
  }
  .mt-h2:hover {
    text-decoration: underline;
  }
  .mt-col {
    text-align: center;
    display: inline-block;
    background: white;
    margin-right: 2px;
    border-radius: 3px;
    padding: 5px 5px 5px;
  }
  .app-main {
    background-color: white;
  }
</style>
